﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace GurLoader.View
{
    /// <summary>
    /// Interaction logic for PreferencesWindow.xaml
    /// </summary>
    [ExcludeFromCodeCoverage]
    public partial class PreferencesWindow : Window
    {
        public PreferencesWindow()
        {
            InitializeComponent();
        }

        private void TreeView_OnSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeViewItem selecteditem = e.NewValue as TreeViewItem;
            if (selecteditem == null) return;

            string selectedPath = BuildTreeViewPath(selecteditem);
            if (string.IsNullOrEmpty(selectedPath)) return;

            foreach (var item in tabcontrol.Items)
            {
                TabItem tabitem = item as TabItem;
                if (tabitem == null) continue;

                if (selectedPath.Equals(tabitem.Header))
                {
                    tabcontrol.SelectedItem = tabitem;
                    break;
                }
            }
        }

        private string BuildTreeViewPath(TreeViewItem selecteditem)
        {
            TreeViewItem currentItem = selecteditem;
            List<string> keys = new List<string>();
            while (currentItem != null)
            {
                keys.Add(currentItem.Header as string);
                currentItem = currentItem.Parent as TreeViewItem;
            }

            keys.Reverse();
            return string.Join("\\", keys);
        }
    }
}
